let AdministratorsModule = require('../models/administrators.js')

/**
 * 导入数据模板 用户、医师、轮播图
 */
let userModule = require('../models/user')
let PhysicianModule = require('../models/physician')
let bannerModule = require('../models/banner')
let appointmentRecordModule = require('../models/appointmentRecord')


// 测试
const cs = (req, res) => {
    res.send({
        Code: 100,
        Text: '测试'
    })
}

// 系统管理员登录
const Glogin = (req, res) => {

    let {
        username,
        password
    } = req.body

    AdministratorsModule.findOne({
        username,
        password
    }).then(data => {
        if (data) {
            let {
                nickname,
                headerimg
            } = data
            res.send({
                Code: 1,
                info: {
                    nickname,
                    headerimg
                }
            })
        } else {
            res.send({
                Code: -1,
            })
        }
    })

}

//查询所有医师
const AllqueryPhy = (req, res) => {

    PhysicianModule.find().then(arr => {
        res.send({
            Code: 1,
            data: arr
        })
    })

}
//查询所有用户
const AllqueryUser = (req, res) => {

    userModule.find().then(arr => {
        res.send({
            Code: 1,
            data: arr
        })
    })

}
//修改医师信息
const updatePhy = (req, res) => {
    let {
        _id,
        username,
        password,
        nickname,
        fullname,
        headerimg,
        sex,
        age
    } = req.body
    PhysicianModule.findOneAndUpdate({
        _id
    }, {
        username,
        password,
        nickname,
        fullname,
        headerimg,
        sex,
        age
    }, {}, function (err, data) {
        if (err) {
            console.log('数据库发生错误')
            res.send({
                Code: -1
            })
        }
        else if (!data) {
            console.log('未查找到相关数据')
            console.log(data)
            res.send({
                Code: 0
            })
        }
        else if (data) {
            console.log('修改数据成功')
            console.log(data)
            res.send({
                Code: 1
            })
        }
    })
}
//修改用户信息
const updateUser = (req, res) => {
    let {
        _id,
        username,
        password,
        nickname,
        fullname,
        headerimg,
        sex,
        age
    } = req.body
    userModule.findOneAndUpdate({
        _id
    }, {
        username,
        password,
        nickname,
        fullname,
        headerimg,
        sex,
        age
    }, {}, function (err, data) {
        if (err) {
            console.log('数据库发生错误')
            res.send({
                Code: -1
            })
        } 
        else if (!data) {
            console.log('未查找到相关数据')
            console.log(data)
            res.send({
                Code: 0
            })
        } 
        else if (data) {
            console.log('修改数据成功')
            console.log(data)
            res.send({
                Code: 1
            })
        }
    })
}
// 删除医师
const delPhy = (req, res) => {
    let {
        _id
    } = req.body
    PhysicianModule.deleteOne({
        _id
    }, function (err) {
        if (!err) {
            res.send({
                Code: 1
            })
        } else {
            res.send({
                Code: -1
            })
        }
})
}
//删除用户信息
const delUser = (req, res) => {
    let { _id } = req.body

    userModule.deleteOne({
        _id
    }, function (err) {
        if (!err) {
            res.send({
                Code: 1
            })
        } else {
            res.send({
                Code: -1
            })
        }
})
}

// 查询所有轮播图
const AllqueryBanner = (req, res) => {
    bannerModule.find().then(arr => {
        res.send({
            Code: 1,
            data: arr
        })
    })
}

// 查询预约记录
const yY = (req,res) => {
    appointmentRecordModule.find().then(arr => {
        res.send({
            Code:1,
            data:arr
        })
    })
}

// 根据_id查询用户
const queryUser = (req,res) => {
    userModule.findOne({_id:req.query.id}).then(data => {
            if (data) {
                res.send({
                    Code: 1,
                    info:data
                })
            } else {
                res.send({
                    Code: 0
                })
            }
    
        }).catch(e => {
            res.send({
                Code: -1,
                data: '错误' + e
            })
        })

}

module.exports = { Glogin, AllqueryPhy, AllqueryUser, cs, AllqueryBanner, updatePhy, updateUser, delPhy, delUser,yY,queryUser }
